#!/usr/bin/env python3
# coding=utf-8

""" 
功能:
    实现 读取输入超时
"""

import sys
import select


def input_timeout(tip, timeout=5):
    print(tip)
    rlist = [sys.stdin]
    cmd = None
    read_ret, write_ret, err_ret = select.select(rlist, [], [], timeout)
    if read_ret:
        for fd in read_ret:
            if fd == sys.stdin:
                cmd = sys.stdin.readline()
            else:
                print("unknow fd")
    else:
        print("read stdin time out")

    return cmd


while True:
    tip = "请输入命令:"
    cmd = input_timeout(tip, 10)
    if cmd is None:
        continue

    print("cmd:", cmd)
    if cmd == "exit":
        break
